package com.example.imoocmall.controller;

import com.example.imoocmall.common.ApiRestResponse;
import com.example.imoocmall.model.pojo.Category;
import com.example.imoocmall.model.request.AddCategoryReq;
import com.example.imoocmall.model.request.UpdateCategoryReq;
import com.example.imoocmall.service.CategoryService;
import com.example.imoocmall.service.impl.UserServiceImpl;
import com.example.imoocmall.model.vo.CategoryVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "管理员对类型目录操作")
public class CategoryController {

    @Autowired
    UserServiceImpl userService;
    @Autowired
    CategoryService categoryService;

    @ApiOperation("管理员添加导航分类目录")
    @PostMapping("/admin/category/add")
    public ApiRestResponse addCategory(@Valid @RequestBody AddCategoryReq addCategoryReq){


//        User currentUser = (User) httpSession.getAttribute(Constant.IMOOC_MALL_USER);
//        if (currentUser==null){
//            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
//        }
//        //校验是否是管理员
//        if (userService.checkAdminRole(currentUser)) {
            categoryService.add(addCategoryReq);
            return ApiRestResponse.success();
//        }else {
//            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
//        }
    }
    @ApiOperation("管理员更新导航分类目录")
    @PostMapping("/admin/category/update")
    public ApiRestResponse updateCategory(@Valid @RequestBody UpdateCategoryReq updateCategoryReq){
//        User currentUser = (User) httpSession.getAttribute(Constant.IMOOC_MALL_USER);
//        if (currentUser==null){
//            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
//        }
//        //校验是否是管理员
//        if (userService.checkAdminRole(currentUser)) {
            Category category = new Category();
            BeanUtils.copyProperties(updateCategoryReq,category);
            categoryService.update(category);
            return ApiRestResponse.success();
//        }else {
//            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
//        }
    }

    @ApiOperation("删除导航分类目录")
    @PostMapping("/admin/category/delete")
    public ApiRestResponse deleteCategory(@RequestParam Integer id){
        categoryService.delete(id);
        return ApiRestResponse.success();
    }

    @ApiOperation("后台目录列表")
    @PostMapping("/admin/category/list")
    public ApiRestResponse listCategoryForAdmin(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        PageInfo pageInfo = categoryService.listForAdmin(pageNum,pageSize);
        return ApiRestResponse.success(pageInfo);
    }

    @ApiOperation("前台目录列表")
    @PostMapping("/category/list")
    public ApiRestResponse listCategoryForAdmin(){
        List<CategoryVo> categoryVos = categoryService.listCategoryForCustomer(0);
        return ApiRestResponse.success(categoryVos);
    }

}
